//该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'



import Axios from 'axios'
import store from '../../store/index'


//引入组件
import MyDynamic from '../components/Dynamic/MyDynamic.vue'
import Square from '../components/square/Square.vue'
import Message from '../components/message/Message.vue'
import Myself from '../components/myself/Myself.vue'
import Mycollect from '../components/myself/Mycollect.vue'//我的收藏

import Activitycontain from '../components/myself/Activitycontain.vue'//引入我的活动具体内容

import Release from '../components/release/Release.vue'
import Follow from '../components/Dynamic/Follow.vue'//关注
import Recommend from '../components/Dynamic/Recommend.vue'//推荐
import Newest from '../components/Dynamic/Newest.vue'//最新

import Food from '../components/square/Food.vue'//美食
import Entertainment from '../components/square/Entertainment.vue'//娱乐
import Job from '../components/square/Job.vue'//兼职
import Chat from '../components/square/Chat.vue'//交流
import Activityone from '../components/common/Activityone.vue'//活动

import Activityyes from '../components/common/Activity.vue'//活动帖子

import Jobone from '../components/common/Job.vue'//兼职帖子
import Alljob from '../components/common/Alljob.vue'//查看用户自己发布的兼职报名情况

import Friend from '../components/square/Friend.vue'//交友
import Market from '../components/square/Market.vue'//市场


import Personinformation from "../components/myself/Personinformation.vue"



import Gender from '../components/common/Gender.vue' //性别选择


import SetUp from '../components/myself/SetUp.vue'
import SearchPage from '../components/common/SearchPage.vue' //搜索
import SearchUser from '../components/common/SearchUser.vue'//搜索的用户结果
import SearchPost from '../components/common/SearchPost.vue'//搜索的用户结果
import SearchRank from '../components/common/SearchRank.vue'//搜索先出来的排行榜
import SearchMore from '../components/common/SearchMore.vue'//搜索让用户选择帖子还是用户



// import Activity from '../components/common/Activity.vue' //活动帖子
import Activity from '../components/square/Activity.vue'

import Notice from '../components/message/Notice.vue'//通知
import Fans from '../components/message/Fans.vue'//粉丝
import PrivateLetter from '../components/message/PrivateLetter.vue'//私信
import ChatPage from '../components/message/ChatPage.vue'//聊天界面
import PostDetail from '../components/common/post/PostDetail.vue'//帖子详情
import CommentDetail from '../components/common/post/CommentDetail.vue'//查看评论的回复内容
// import Login from '../components/common/Login.vue'//登录

import Tc from '../components/common/Tc.vue'//弹出层




import School from '../components/common/School.vue'//选择学校





import Text from '../components/common/Text.vue'//测试


import Themself from '../components/myself/Themself.vue'//别人的主页


import Area from '../components/common/Area.vue'//省市区的选择

import Ifyes from '../components/common/Ifyes.vue'//省市区的选择

import Mylike from '../components/myself/Mylike.vue' //我的喜欢
import Myhistory from '../components/myself/Myhistory.vue' //我的兼职
import Myrelease from '../components/myself/Myrelease.vue' //我的动态

import BackstageApp from '../components/backstage/BackstageApp.vue'
import Useradmin from '../components/backstage/User/Useradmin.vue'
import Trendadmin from '../components/backstage/Trend/Trendadmin.vue'
import Workadmin from '../components/backstage/Work/Workadmin.vue'
import Activeadmin from '../components/backstage/Active/Activeadmin.vue'
import Marketadmin from '../components/backstage/Market/Marketadmin.vue'
import Appeal from '../components/backstage/Appeal/Appeal.vue'
import BackstageMessage from '../components/backstage/Message.vue'

import router from '../router/index'

//创建并抛出路由器
 const routers =  new VueRouter({
    store,
    routes: [
        //默认路由
        {
            path: '/',
            redirect: 'dynamic/recommend',
            name: 'recommend',
        },

        {
            path: '/gender',
            component: Gender
        },
        {
            path: '/jobone',
            component: Jobone
        }, 
        {
              path: '/alljob',
              component: Alljob
        },
        
        {
            path: '/activityyes',
            component: Activityyes
        },
        {
            path: '/mycollect',
            component: Mycollect
        },
        
        {
            path: '/activitycontain',
            component: Activitycontain
        },
        // {
        //     path: '/activity',
        //     component: Activity
        // },
        {
            path: '/activityone/:id',
            component: Activityone
        },
        
        

        //其它路由

        //查看子评论
        {
            path: '/comment-detail/:id',
            component: CommentDetail
        },

        //搜索页面
        {
            path: '/search-page',
            component: SearchPage,
            children: [
                { path: 'search-rank', component: SearchRank },
                {
                    path: 'search-more',
                    component: SearchMore,
                    children: [
                        { path: 'user', component: SearchUser },
                        { path: "post", component: SearchPost }
                    ]
                }
            ]
        },

        {
            path: '/backstage',
            component: BackstageApp,
            children: [
                {
                    path: '/backstage',
                    component: Useradmin
                },
                {
                    path: '/backstage/useradmin',
                    component: Useradmin
                },
                {
                    path: '/backstage/trendadmin',
                    component: Trendadmin
                },
                {
                    path: '/backstage/workadmin',
                    component: Workadmin
                },
                {
                    path: '/backstage/activeadmin',
                    component: Activeadmin
                },
                {
                    path: '/backstage/marketadmin',
                    component: Marketadmin
                },
                {
                    path: '/backstage/appeal',
                    component: Appeal
                },
                {
                    path: '/backstage/message',
                    component: BackstageMessage
                },
            ]
        },

        {
            path: '/setup',
            component: SetUp
        },

        {
            path: '/area',
            component: Area
        },



        {
            path: '/text',
            component: Text
        },

        {
            path: '/mylike',
            component: Mylike
        },
        {
            path: '/myrelease',
            component: Myrelease
        },
        {
            path: '/myhistory',
            component: Myhistory
        },
        {
            path: '/personinformation',
            component: Personinformation
        },
        {
            path: '/postDetail/:bodyId',
            component: PostDetail
        },
        {
            path: '/tc',
            component: Tc,
        },
        {
            path: '/themself/:id',
            component: Themself,
        },

        //活动部分
        {
            path: '/dynamic',
            component: MyDynamic,
            children: [
                { path: '/dynamic/follow', component: Follow},
                { path: '/dynamic/recommend', component: Recommend },
                { path: '/dynamic/newest', component: Newest },
            ],
            redirect: '/dynamic/recommend',
            
        },
        //广场部分
        {
            path: '/square',
            component: Square,
            redirect:'/square/food',
            children: [
                { path: 'food', component: Food },
                { path: 'entertainment', component: Entertainment },
                { path: 'job', component: Job },
                { path: 'chat', component: Chat },
                { path: 'activity', component: Activity },
                { path: 'friend', component: Friend },
                { path: 'market', component: Market },
            ]
        },
        {
            path: '/release',
            component: Release
        },
        //消息部分
        {
            path: '/message',
            component: Message,
            redirect:'/message/notice',
            children: [
                { path: 'notice', component: Notice },
                { path: 'fans', component: Fans },
                { path: 'private-letter', component: PrivateLetter },
            ]
        },
        { path: '/chat-page/:toUserId', component: ChatPage },//两个人的聊天窗口
        
        
        {
            path: '/myself',
            component: Myself
        },
        //性别选择
        {
            path: '/ifyes',
            component: Ifyes
        }, 
        {
            path: '/login',//path:网页具体路径
            name:"login",
            //来这里
            component: () => import('../components/common/Login.vue'),
            // component: Login
        },

        {
            path: '/school',
            component: School
        },


    ],

})


// router.beforeEach 注册一个全局前置守卫
routers.beforeEach((to, from, next) => {
    console.log(to);
    console.log(from);
    next();
})
export default routers;


// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
// to 将要访问的路径
// from 代表从哪个路径跳转而来
// next 是一个函数，表示放行
// next() 放行 next('/login') 强制跳转
routers.beforeEach((to,from,next) => {
    if (to.path === '/login') {
        //如果访问的路径是登录页面,那么进行放行
        next();
    } else {
        //如果访问的路径不是登录页面是其他页面,那么进行获取token
        //从session中获取token的值进行赋值
        let token = window.sessionStorage.getItem('token');
        
        if (token === 'null' || token === '') {
            //如果token为null/为空  进入登录页面
            next('/login');
        } else {
            // 否则不为空就进行放行
            next();
        }
    }
});





